var compression = require('compression');
var express = require('express');
var opn = require('opn')
var proxyMiddleware = require('http-proxy-middleware')
var app = express();
//分发请求
var proxyTable = {
      '/public': {
        target: 'http://140.143.13.75:8888',
        changeOrigin: true
      },
    }
app.get('', function (req, res) {
  res.sendfile("./dist/index.html")
});
//支持前端路由 history模式
app.use(require('connect-history-api-fallback')())
app.use(compression())

Object.keys(proxyTable).forEach(function (context) {
  var options = proxyTable[context]
  if (typeof options === 'string') {
    options = { target: options }
  }
  app.use(proxyMiddleware(context, options))
})

//确保static为静态目录
//app.use(express.static('./distx'));
app.use('/static',express.static('./dist/static'));
var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;
  var url = 'http://localhost:' + port
  console.log('Example app listening at http://%s:%s', host, port);
  opn(url)
});
